package com.yh.blog.controller.admin;

import cn.hutool.json.JSONObject;
import com.yh.blog.util.QiNiuUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("/img")
public class UploadController {
    @Autowired
    private QiNiuUtils qiNiuUtils;

    @RequestMapping("/imgUpload")
    public void imageUpload(@RequestParam(value = "editormd-image-file",required = true) MultipartFile[] files,
                            HttpServletRequest request, HttpServletResponse response)  {
//        设置字符集
        response.setCharacterEncoding("Utf-8");
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter wirte=null;
        JSONObject json=new JSONObject();

        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                try {
                    wirte = response.getWriter();
                    FileInputStream fileInputStream = (FileInputStream) files[i].getInputStream();
                    String url = "http://" + qiNiuUtils.upLoadQiNiuYun(fileInputStream);
                    //输出url上传后的,可以复制url到浏览器访问
                    System.out.println("url=" + url);
                    json.set("success", 1);
                    json.set("message", "上传成功");
                    json.set("url", url);
                    try {
                        //延迟两秒让七牛云缓一下
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    json.set("success", 0);
                    json.set("message", "上传失败");
                    e.printStackTrace();
                } finally {
                    wirte.print(json);
                    wirte.flush();
                    wirte.close();
                }
            }
        }


//        try {
//            wirte =response.getWriter();
//            //文件存放的路径
//            //得到工程的路径：
//            System.out.println("得到工程的路径："+System.getProperty("user.dir"));//user.dir指定了当前的路径
//            //得到工程目录：
//            System.out.println( "得到工程目录："+request.getSession().getServletContext().getRealPath("")); //参数可具体到包名
//            //得到IE地址栏地址  request.getRequestURL()
//            System.out.println( "得到IE地址栏地址  :"+ request.getRequestURL());
//            // 得到相对地址：request.getRequestURI()
//            System.out.println( "得到相对地址："+ request.getRequestURI());
//
//            String path=System.getProperty("user.dir")+"\\src\\main\\resources\\static\\upload\\";
//
//            String fileName = file.getOriginalFilename();
//            fileName= UUID.randomUUID()+fileName.substring(fileName.indexOf("."),fileName.length());
//            String destFileName=path+fileName;
//            System.out.println("path:"+path);
//
//            File destFile = new File(destFileName);
//            if(!destFile.exists()){
//                destFile.getParentFile().mkdirs();
//            }
//
//            String url = "http://localhost:8080"
//                    + "//upload/"
//                    + fileName;
//            System.out.println("url:"+url);
//            file.transferTo(destFile);
//            json.put("success", 1);
//            json.put("message", "上传成功");
//            json.put("url", url);
//        } catch (IllegalStateException | IOException e) {
//            json.put("success", 0);
//            json.put("message", "上传失败");
//        } finally {
//            wirte.print(json);
//            wirte.flush();
//            wirte.close();
//        }
    }
}
